package com.HuangJiaQing.www.service.impl;

import com.HuangJiaQing.www.dao.CartMapper;
import com.HuangJiaQing.www.dao.GoodsMapper;
import com.HuangJiaQing.www.model.Orders;
import com.HuangJiaQing.www.dao.OrdersMapper;
import com.HuangJiaQing.www.model.*;
import com.HuangJiaQing.www.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author HONOR
 * @date 2022/7/29 15:18
 */
@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private HttpSession session;
    @Autowired
    private GoodsMapper goodsMapper;

    public Boolean insertOrders(final Integer id, String method) {
        Cart cart = cartMapper.selectByPrimaryKeyWithGoods(id);
        Goods goods = goodsMapper.selectByPrimaryKey(cart.getGoodsId());
        goods.setNumber(goods.getNumber() - cart.getGoodsNumber());
        if (goods.getNumber() < 0) {
            return false;
        } else if(goods.getNumber()==0) {
            goodsMapper.deleteByPrimaryKey(goods.getGoodsId());
        } else{
            goodsMapper.updateByPrimaryKeySelective(goods);
        }
        User user = (User) session.getAttribute("user");
        Orders orders = new Orders();
        orders.setUserId(user.getUserId());
        orders.setUsername(user.getUsername());
        orders.setGoodsId(cart.getGoodsId());
        orders.setMarketerName(cart.getGoods().getMarketersName());
        orders.setMoney(cart.getGoods().getGoodsPrice() * cart.getGoodsNumber());
        if ("快跑配送".equals(method)) {
            orders.setIsAccept(0);
        }
        orders.setMethod(method);
        int i = ordersMapper.insertSelective(orders);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    public List<Orders> selectOrders(Model model) {
        User user = (User) session.getAttribute("user");
        OrdersExample ordersExample = new OrdersExample();
        ordersExample.createCriteria().andUserIdEqualTo(user.getUserId());
        List<Orders> orders = ordersMapper.selectByExampleWithGoods(ordersExample);
        return orders;
    }

    public List<Orders> selectOrdersNeedCourier() {
        OrdersExample ordersExample = new OrdersExample();
        ordersExample.createCriteria().andIsAcceptEqualTo(0);
        List<Orders> orders = ordersMapper.selectByExampleWithGoods(ordersExample);
        return orders;
    }

    public Orders selectOrdersById(Integer id) {
        Orders orders = ordersMapper.selectByPrimaryKeyWithGoods(id);
        return orders;
    }

    public List<Orders> selectOrdersNeedCollect() {
        Marketers marketers = (Marketers) session.getAttribute("marketers");
        OrdersExample ordersExample = new OrdersExample();
        ordersExample.createCriteria().andMarketerNameEqualTo(marketers.getMarketersName()).andIsAcceptEqualTo(0);
        List<Orders> orders = ordersMapper.selectByExampleWithGoods(ordersExample);
        return orders;
    }

    public void updateOrdersIsCollect(Integer id) {
        Orders orders = new Orders();
        orders.setOrdersId(id);
        orders.setIsCollect(1);
        ordersMapper.updateByPrimaryKeySelective(orders);
    }

    public List<Orders> selectOrdersCollected() {
        OrdersExample ordersExample = new OrdersExample();
        ordersExample.createCriteria().andIsCollectEqualTo(1).andIsAcceptEqualTo(0);
        List<Orders> orders = ordersMapper.selectByExampleWithGoods(ordersExample);
        return orders;
    }


}
